package com.cykj.v_m_sprintboot.controller;

import com.cykj.v_m_sprintboot.bean.PageInf;


import com.cykj.v_m_sprintboot.bean.TbOperator;
import com.cykj.v_m_sprintboot.service.TbOpratorService;
import com.cykj.v_m_sprintboot.tool.JsonXMLUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.HashMap;

import java.util.Map;
import java.util.*;

/**
 * @author: wei
 * @date: 2021/9/23 23:47
 */

@RestController
public class TbOperatorController {
    @Autowired
    private TbOpratorService tbOpratorService;

    //获取账号管理数据表格的方法
    @RequestMapping("/getOperatorList")
   @ResponseBody
    public Map<String, Object> getOperatorList(@RequestBody PageInf pageInf)  {
        System.out.println("aaaa");
//        System.out.println("qqqqqqqq"+tbOperator.getOperatorId()+pageInf.getPage());
        Map<String,Object> nameMap = new HashMap<>();
        nameMap.put("PageInf", pageInf);
        nameMap.put("count", tbOpratorService.findid());
        nameMap.put("operatorList", tbOpratorService.findOperator(new RowBounds((pageInf.getPage() -1) * pageInf.getSize(),pageInf.getSize())));
        return nameMap;
        }

    //删除数据的方法
    @RequestMapping("delOperator")
    @ResponseBody
    public String delOperator(int operatorId){
        System.out.println(operatorId);
        boolean flag=tbOpratorService.delOperator(operatorId,2);
        if (flag==true){
            return "删除成功";
        }
        return "删除失败";
    }

    //修改数据的方法
    @RequestMapping("changOperator")
    @ResponseBody
    public boolean  changOperator(@RequestBody TbOperator tbOperator){
        System.out.println(tbOperator.getOperatorId());
        int id = (int) tbOperator.getOperatorId();
        boolean flag=tbOpratorService.changOperator(id, tbOperator.getOperatorAccount(),
                tbOperator.getOperatorTime(), tbOperator.getOperatorName(),tbOperator.getWarehouseStateId(), tbOperator.getOperatorPhone(),
                tbOperator.getOperatorAddress(),tbOperator.getOperatorLicence());
        if (flag){
            return true;
        }else {
            return false;
        }

    }

//    //账号管理查询
////    @RequestMapping("/onSelect")
////    @ResponseBody
////    public Map<String, Object> onSelect(@RequestBody TbOperator tbOperator)  {
////        System.out.println("查询一下"+tbOperator);
////        String operatorName =  tbOperator.getOperatorName();
////        System.out.println("冲冲冲"+operatorName);
//////        System.out.println("qqqqqqqq"+tbOperator.getOperatorId()+pageInf.getPage());
////        Map<String,Object> nameMap1 = new HashMap<>();
////       nameMap1.put("PageInf", pageInf);
////        nameMap1.put("count", tbOpratorService.findid());
////        nameMap1.put("SelectList", tbOpratorService.findonSelect(operatorName));
////        return nameMap1;
////    }

//    运营商账号管理的名字查询
@RequestMapping("/onSelect")
@ResponseBody
    public Map<String,Object> findSite(@RequestBody Map<String,Object> map) throws Exception {
        PageInf pageInf = null;
        TbOperator tbOperator = null;
        if (map.get("pageInf")!=null){
            pageInf = JsonXMLUtils.map2obj((Map<String, Object>)map.get("pageInf"),PageInf.class);
        }
        if (map.get("tbOperator")!=null){
            tbOperator = JsonXMLUtils.map2obj((Map<String, Object>) map.get("tbOperator"),TbOperator.class);
        }
        System.out.println("查询数据");
        System.out.println(tbOperator.getOperatorName()+" "+pageInf.getPage());
        Map<String,Object> nameMap = new HashMap<>();
        String operatorName =  tbOperator.getOperatorName();
        nameMap.put("pageConfig",pageInf);
        nameMap.put("count", tbOpratorService.findid());
        nameMap.put("SelectList",tbOpratorService.findonSelect(operatorName,new RowBounds((pageInf.getPage() -1) * pageInf.getSize(),pageInf.getSize())));
        return nameMap;
    }

    //运营商账号重置密码
    @RequestMapping("ChangePwdOperator")
    @ResponseBody
    public boolean ChangePwdOperator(@RequestBody TbOperator tbOperator){
        System.out.println("我进来修改密码啦");
        int id = (int) tbOperator.getOperatorId();
        String operatorPassword = "123456" ;
        System.out.println("修改密码："+operatorPassword);
        boolean flag=tbOpratorService.ChangePwdOperator(id,operatorPassword);
        if (flag){
            return true;
        }else {
            return false;
        }
    }

    //文件上传ftp
//    @PostMapping(value = "/upImg")
//    @ResponseBody
//    public Object upImg(HttpServletRequest request, @RequestParam("fileName") MultipartFile file) throws IOException {
//        request.setCharacterEncoding("UTF-8");
//        System.out.println("sssssss");
//        //获取上传的文件流
//        InputStream inputStream = file.getInputStream();
//        //获取上传的文件名
//        String filename = file.getOriginalFilename();
//        double fileSize = file.getSize();
//        double fileSizeMB = new BigDecimal(fileSize/1024/1024).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//        System.out.println("后台床过来的名字:"+filename);
//        System.out.println("文件长度为："+fileSizeMB);
//        //截取后缀
//        String suffix = filename.substring(filename.lastIndexOf("."));
//        //使用UUID拼接后缀，定义一个不重复的文件名
//        String finalname = UUID.randomUUID()+suffix;
//
//        //调用自定义的FTP工具类上传文件
//        boolean fing = FtpUtil.uploadFile(finalname,inputStream);
////        fileImgUrl = "121.43.147.36/"+finalname;
////        System.out.println("名字是多少"+fileImgUrl);
//        if (fing==true){
////            securityVideo.setVideoName(filename);
////            securityVideo.setVideoSize(fileSizeMB);
////            securityVideo.setVideoUrl(finalname);
////            securityVideo.setCreationTime(df.format(new Date()));
////            lpwVideoService.videoadd(securityVideo);
//            System.out.println("添加成功");
////            return "上传成功";
////        }else{
////            return "文件上传失败";
//        }
//        return "1";
//    }




}
